;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.DealerPlanData", [])
        .controller("DealerPlanDataCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", "$q", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService, $q) {
            $scope.handleMultiInput = function (e) {
                $scope.condition.materialsNoListStr = UserContextService.MultiInput(e.target.value);
            };

            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];

            $scope.tbConf = {
                fixedWidth: 140,
                scrollWidth: 1300,
                tableCol: [
                    {
                        name: "大区",
                        width: 100,
                        dataField: "belongsRegion",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "储备方-经销商",
                        width: 150,
                        dataField: "reserveStationName",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件编号",
                        width: 120,
                        dataField: "newProductCode",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "newProductName",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件分类",
                        width: 150,
                        dataField: "materialsType",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "CRM—储备数量",
                        width: 120,
                        dataField: "stockCnt",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "机型",
                        width: 120,
                        dataField: "newProductModelNames",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "新件价格",
                        width: 120,
                        dataField: "newPartsCost",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "是否直投",
                        width: 100,
                        dataField: "isDirectCastShow",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 100,
                        isShow: true,
                        isFixed: true,
                    }
                ]
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: {left: $scope.tbConf.fixedWidth + "px"},
                    scrollLeftWidth: {width: $scope.tbConf.fixedWidth + "px"}
                };
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                var service = "device";
                var reqUrl = "/dealerPlanCrm/list";
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: $scope.condition
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.isDirectCastShow = $filter("TrueOrFalse")(item.isDirectCast);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.delData = function (item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    handleDelData([item.id]);
                })
            };

            $scope.delDataBatch = function () {
                const selectedDatas = $scope.dataList.filter(df => df.isChecked && df.delFlag == 0);
                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }

                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    handleDelData(selectedDatas.map(sm => sm.id));
                })
            };

            function handleDelData(idList) {
                var url = '/dealerPlanCrm/remove';
                ybHttp.Post('device', url, idList).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.search(1);
                });
            }


            $scope.revokeDelData = function (item) {
                ybWidget.ShowMessage("确定要恢复删除的数据吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post("device", "/dealerPlanCrm/unRemove", [item.id]).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        item.delFlag = 0;
                        ybWidget.ShowToaster('success', '操作成功');
                    })
                }, function () {

                })
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
    ;
})(window, window.angular);
